package com.device.operating.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ToolsStr {

	public static List<Integer> getnumlist(String str){
		Pattern p = Pattern.compile("[0-9\\.]+");
		Matcher m = p.matcher(str);
		List<Integer> listnum=new ArrayList<>();
		while(m.find()){
			listnum.add(new Integer(m.group()).intValue());
		}
		return listnum;
	}
	
	public static List<String[]> getippart(String ippart){
		List<String[]> list=new ArrayList<>();
		String[] st=ippart.split("-");
		if(st.length==1){
			String[] str=st[0].split("\\.");
			if(str.length!=4)return list;
			else list.add(str);
		}else if(st.length==2){
			String[] str1=st[0].split("\\.");
			String[] str2=st[1].split("\\.");
			if(str1.length!=4||str2.length!=4)return list;
			else{
				list.add(str1);
				list.add(str2);
			}
		}else return list;
		return list;
	}
	public StringBuffer creatsql(String fx,String zf){
		List<String[]> listfx=this.listfx(fx);
		List<String[]> listzf=this.listzf(zf);
		if(!zf.equals("")){
			
		}
		return null;
	}
	public static List<String[]> listzf(String zf){
		List<String[]> listzf=new ArrayList<>();
		if(zf.equals(""))return listzf;
		String[] zifu=zf.split(":");
		for(int i=0;i<zifu.length;i++){
			listzf.add(zifu[i].split(","));
		}
		return listzf;
	}
	public static List<String[]> listfx(String fx){
		List<String[]> listfx=new ArrayList<>();
		if(fx.equals(""))return listfx;
		String[] fuxuan=fx.split(":");
		if(fuxuan.length==1){
			listfx.add(fuxuan[0].split(","));
			return listfx;
		}
		int i=0,t=0;
		while(i<fuxuan.length-1){//遍历所有复选字段
			t=i;
			StringBuffer sb=new StringBuffer();
			sb.append(fuxuan[i].split(",")[0]+","+fuxuan[i].split(",")[1]);//ss[0]为字段ID，ss[1]为值，第一个值为字段ID
			boolean flag=false;
			for(int j=i+1;j<fuxuan.length;j++){
				if(fuxuan[j].split(",")[0].equals(fuxuan[i].split(",")[0])){
					sb.append(","+fuxuan[j].split(",")[1]);//加入取值
					flag=true;
					t=j;
				}
			}//for fuxuan
			if(flag==false&&i+1==fuxuan.length-1){
				listfx.add(fuxuan[i+1].split(","));//得到List,字段ID与对应值sc[0],为ID，sc[1]以上为值				
			}
			String[] sc=sb.toString().split(",");
			listfx.add(sc);//得到List,字段ID与对应值sc[0],为ID，sc[1]以上为值
			i=t+1;
			}//遍历所有复选字段
		return listfx;
	}
	
	public static List<String[]> listsz(String sz){
		List<String[]> listsz=new ArrayList<>();
		if(sz.length()==0)return listsz;
		String[] shuzi=sz.split(":");
		for(int i=0;i<shuzi.length;i++){
			listsz.add(shuzi[i].split(","));
		}
		return listsz;
	}
	
	public static List<String[]> listriqi(String riqi){
		List<String[]> listsz=new ArrayList<>();
		if(riqi.length()==0)return listsz;
		String[] rq=riqi.split(";");//日期用;避免和：重叠
		for(int i=0;i<rq.length;i++){
			listsz.add(rq[i].split(","));
		}
		return listsz;
	}
	
	/**
	 * 根据复选，单选，字符，数字，日期等查询条件，和归属，排序等条件拼接SQL
	 * @param fx
	 * @param zf
	 * @param danxuan
	 * @param shuzi
	 * @param dvbelong
	 * @param ob
	 * @return
	 */
	public static String[] creatsql(String fx,String zf,String danxuan,String shuzi,String riqi,String dvbelong,String[] ob,String type){		
		List<String[]> listfx=listfx(fx);
		List<String[]> listzf=listzf(zf);
		List<String[]> listdx=listzf(danxuan);
		List<String[]> listsz=listsz(shuzi);
		List<String[]> listriqi=listriqi(riqi);
		String[] sql=new String[2];
		String newStr=dvbelong.substring(0, 1).toUpperCase()+dvbelong.replaceFirst("\\w","");
		StringBuffer sb=new StringBuffer();
		sql[0]="";
		sql[1]="";
		if(type.equals("0")){
			sql[0]="select "+dvbelong+" from "+newStr+" "+dvbelong;
			sql[1]="select count(*) from "+newStr+" "+dvbelong;
		}else if(type.equals("1")){
			sql[0]="select "+dvbelong+"_approvation.*,"+
		dvbelong+"_info.creatdate,"+dvbelong+"_info.completedate,"+
					dvbelong+"_info.lastedmin,"+dvbelong+"_info.overtime,"+
		dvbelong+"_info.workstatus"+" from "+dvbelong+"_approvation,"+
					dvbelong+"_info where "+dvbelong+"_approvation.businesskey="+dvbelong+"_info.businesskey";
			sql[1]="select count(*) from "+dvbelong+"_approvation,"+
					dvbelong+"_info where "+dvbelong+"_approvation.businesskey="+dvbelong+"_info.businesskey";
		}else if(type.equals("2")){//排名统计
			sql[0]="select "+dvbelong+"_approvation.username,"+dvbelong+"_approvation.gpname,count(distinct "+dvbelong+"_approvation.businesskey) from "+dvbelong+"_approvation,"+
					dvbelong+"_info where "+dvbelong+"_approvation.businesskey="+dvbelong+"_info.businesskey";
			sql[1]="select count(distinct "+dvbelong+"_approvation.userId) from "+dvbelong+"_approvation,"+
					dvbelong+"_info where "+dvbelong+"_approvation.businesskey="+dvbelong+"_info.businesskey";
		}
		int count=0;
		if(type.equals("0")&&(listfx.size()!=0||listzf.size()!=0||listdx.size()!=0||listsz.size()!=0||listriqi.size()!=0)){
			sb.append(" where ");
		}else if((type.equals("1")||type.equals("2"))&&(listfx.size()!=0||listzf.size()!=0||listdx.size()!=0||listsz.size()!=0||listriqi.size()!=0)){
			sb.append(" and ");
		}
		if(listfx.size()!=0){
			count=0;
			for(String[] fuxuan:listfx){
				String fieldcode=fuxuan[0];	
				sb.append("(");
				for(int i=1;i<fuxuan.length;i++){
					sb.append(fieldcode+"='"+fuxuan[i]);
					if(i!=fuxuan.length-1){
						sb.append("' or ");
					}else
						sb.append("'");
				}
				sb.append(") ");
				count++;
				if(count!=listfx.size()){
					sb.append("and ");
				}
			}
		}
		if(listzf.size()!=0){
			if(count!=0)
			sb.append("and ");
			count=0;
			for(String[] zifu:listzf){
				String fieldcode=zifu[0];	
				sb.append(fieldcode+" like '%"+zifu[1]+"%'");
				count++;
				if(count!=listzf.size())
				sb.append(" and ");
			}
		}
		if(listdx.size()!=0){
			if(count!=0)
			sb.append("and ");
			count=0;
			for(String[] zifu:listdx){
				String fieldcode=zifu[0];						
				sb.append(fieldcode+"='"+zifu[1]+"'");
				count++;
				if(count!=listdx.size())
				sb.append(" and ");
			}
		}
		if(listsz.size()!=0){
			if(count!=0)
			sb.append("and ");
			count=0;
			for(String[] sz:listsz){
				String fieldcode=sz[0];	
				sb.append("(");
					if(sz.length==3){						
						sb.append(fieldcode+">="+sz[1]+" and "+fieldcode+"<="+sz[2]);
					}else{//长度为2，没选最大
						sb.append(fieldcode+">="+sz[1]);
					}
				sb.append(") ");
				count++;
				if(count!=listsz.size()){
					sb.append("and ");
				}
			}
		}
		if(listriqi.size()!=0){
			if(count!=0)
			sb.append("and ");
			count=0;
			for(String[] rq:listriqi){
				String fieldcode=rq[0];	
				sb.append("(");
					if(rq.length==3){						
						sb.append(fieldcode+">='"+TimeStr.getfordate(rq[1])+"' and "+fieldcode+"<='"+TimeStr.getfordate(rq[2])+"'");
					}else{//长度为2，没选最大
						sb.append(fieldcode+">='"+TimeStr.getfordate(rq[1])+"'");
					}
				sb.append(") ");
				count++;
				if(count!=listriqi.size()){
					sb.append("and ");
				}
			}
		}
		sql[1]=sql[1]+sb.toString();
		if(ob!=null){
			sb.append(" order by ");
			for(int i=0;i<ob.length;i++){
				sb.append(ob[i]);
				if(i+1!=ob.length)
					sb.append(",");
			}
		}
		//sb.append(" order by dajuxiang,jifang,oltnum");
		sql[0]=sql[0]+sb.toString();
		
		if(type.equals("2")){
			sql[0]=sql[0]+" group by "+dvbelong+"_approvation.userId order by count(*) desc";
		}
		System.out.println(sql[1]);
		System.out.println(sql[0]);
		return sql;
	}
	
	
	public static StringBuffer creatmapsql(String dvtable,String danxuan,String stype,String reqfield){
		StringBuffer sb=new StringBuffer();
		String type="";
		if(stype.equals("0")){
			type="count(*)";
		}else
			type="sum("+reqfield+")";
		sb.append("select juxiang,jxId,"+type+" from "+dvtable);
		if(danxuan.equals("0")){//无搜索条件
			sb.append(" group by juxiang order by "+type+" desc");
		}else{
			sb.append(" where ");
			String[] fx=danxuan.split(":");
			for(int i=0;i<fx.length;i++){
				String str[]=fx[i].split(",");
				sb.append(str[0]+"='"+str[1]+"' ");
				if(i<fx.length-1){
					sb.append("and ");
				}
			}
			sb.append(" group by juxiang order by "+type+" desc");
		}
		return sb;
	}
	
	public static String getfhsql(String sqlhtml){	
		sqlhtml =sqlhtml.replaceAll("&lt;", "<");
		sqlhtml =sqlhtml.replaceAll("&gt;", ">");
		sqlhtml =sqlhtml.replaceAll("&amp;", "&");	
		return sqlhtml;
	}
	
	public static String getbqsql(String sqlhtml){
		sqlhtml =sqlhtml.replaceAll("<br />", " ");
		sqlhtml =sqlhtml.replaceAll("&nbsp;", " ");
		String fh=sqlhtml.substring(sqlhtml.length()-1,sqlhtml.length());
		if(fh.equals(";")||fh.equals("；")){
			sqlhtml=sqlhtml.substring(0,sqlhtml.length()-1);
		}			
		return sqlhtml;
	}
	
//	public static String getcronstr(Dsinfo ds){
//		String miao=ds.getMiao();
//		String fen=ds.getFen();
//		String shi=ds.getShi();
//		String tian=ds.getTian();
//		String yue=ds.getYue();
//		String zhou=ds.getZhou();
//		String nian=ds.getNian();
//		String[] z=zhou.split("");
//		zhou="";
//		for(int i=1;i<z.length;i++){
//			if(isNumeric(String.valueOf(z[i]))){
//				if(Integer.valueOf(z[i])==7){
//					z[i]="1";
//				}else{
//					z[i]=Integer.valueOf(z[i])+1+"";
//				}
//			}
//			zhou=zhou+z[i];
//		}
//		return miao+" "+fen+" "+shi+" "+tian+" "+yue+" "+zhou+" "+nian;
//	}
	
	public static boolean isNumeric(String str){
		for (int i = str.length();--i>=0;){   
			if (!Character.isDigit(str.charAt(i))){
				return false;
			}
		}
		return true;
	}
	
}
